import { defineStore } from 'pinia';
import { login,register } from '@/api/login'

export const useUserStore = defineStore('user', {
    state: () => {
        return { userInfo: null, token: null,openId:null,isSxToken:0 };
    },
    actions: {
        setUserInfo(uInfo) {
            this.userInfo = uInfo
        },
        setToken(t) {
            this.token = t
        },
        setOpenId(openId) {
            this.openId = openId
        },
        async wxLogin({ phone, openId, code }) {
            if (!phone || !openId) {
                uni.showToast({
                    title: '手机号或open获取失败',
                    icon: 'none'
                });
                return
            }
            const { data } = await login({ phone, openId, code })
            this.token = data.access_token
            uni.switchTab({
                url: '/pages/home/index'
            })
        },
        async wxRegister({ phoneNumber, openId, smsCode,uuid,nickName,avatar }) {
            if (!phoneNumber || !openId) {
                uni.showToast({
                    title: '手机号或open获取失败',
                    icon: 'none'
                });
                return
            }
            const { code,data,msg } = await register({ phoneNumber, openId, smsCode,uuid,nickName,avatar })
            if(code==200){
                uni.switchTab({
                url: '/pages/home/index'
                })   
            }else{
                uni.showToast({
                    title: msg,
                    icon: 'none'
                });
            }
        },
        changeIsSxToken(data) {
			this.isSxToken += data
		}
    },
    // unistorage: {
    //     key: "token",
    //     paths: ["token","openId"],
    // },
    unistorage: [
        {
          key: "token",
          paths: ["token"],
        },
        {
          key: "openId",
          paths: ["openId"],
        },
      ],
});
